package com.sgai.zero_robot.ui.control;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.sgai.zero_robot.R;
import com.sgai.zero_robot.utils.LogUtil;
import com.siyi.imagetransmission.connection.ConnectionManager;

public class VideoPlayer extends FrameLayout {
    private final String TAG = "VideoPlayer";
    private final ConstraintLayout root;
    private final TextView tvDisconnect;
    private final ImageView ivStroke;
    private TextureView textureView;
    private int index;
    private SurfaceTexture mSurfaceTexture;
    Surface mSurface;


    public VideoPlayer(@NonNull Context context) {
        super(context);
        View inflate = View.inflate(context, R.layout.video_player, this);
        root = inflate.findViewById(R.id.root);
        tvDisconnect = inflate.findViewById(R.id.tv_small_video_disconnect);
        ivStroke = inflate.findViewById(R.id.iv_stroke);
    }

    public int getIndex() {
        return index;
    }
    public void showDisconnect(boolean isShow){
        if (isShow){
            tvDisconnect.setVisibility(VISIBLE);
        }else{
            tvDisconnect.setVisibility(GONE);
        }
    }
    public void showStroke(boolean showStroke){
        if (showStroke){
            ivStroke.setVisibility(VISIBLE);
        }else{
            ivStroke.setVisibility(GONE);
        }
    }
    /**
     * 1是第一个视频
     * 2是第二个视频
     * @param index
     */
    public void initPlayer(int index,boolean showStroke){
        if (showStroke){
            ivStroke.setVisibility(VISIBLE);
        }
        this.index = index;
        textureView = new TextureView(getContext());
        ConstraintLayout.LayoutParams layoutParams2 = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        textureView.setLayoutParams(layoutParams2);
        textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
                LogUtil.d(TAG, "onSurfaceTextureAvailable: ");
                if (mSurfaceTexture != null) {
                    textureView.setSurfaceTexture(mSurfaceTexture);
                } else {
                    mSurfaceTexture = surface;
                    mSurface = new Surface(surface);
                    if (index == 1){
                        ConnectionManager.getInstance(getContext()).notifySurfaceCreate(mSurface);
                    }else if (index == 2){
                        ConnectionManager.getInstance(getContext()).notifySecondSurfaceCreate(mSurface);
                    }
                }
            }

            @Override
            public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
                LogUtil.d(TAG, "onSurfaceTextureDestroyed: ");
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {

            }
        });
        root.addView(textureView, 0);
    }

    public TextureView getTextureView() {
        return textureView;
    }

    public Surface getmSurface() {
        return mSurface;
    }
}
